<?php

/**
 * traitement actions.
 *
 * @package    appcemhti
 * @subpackage traitement
 * @author     Steve Cancès
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class traitementActions extends sfActions
{
    protected function droitLecture()
    {
        $droitUtilisateur = $this->lot->idDroitSurLot($this->getUser()->getGuardUser()->getId());
        $this->forwardUnless($droitUtilisateur == 3 || $droitUtilisateur == 2, 'sfGuardAuth', 'secure');
    }
    
    protected function droitEcriture()
    {
        $droitUtilisateur = $this->lot->idDroitSurLot($this->getUser()->getGuardUser()->getId());
        $this->forwardUnless($droitUtilisateur == 3, 'sfGuardAuth', 'secure');
    }

    public function executeShow(sfWebRequest $request)
    {
        $this->forward404Unless($this->traitement = Doctrine_Core::getTable('traitement')->find(array($request->getParameter('id_traitement'))), sprintf('Object traitement does not exist (%s).', $request->getParameter('id_traitement')));

        $this->lot = $this->traitement->getLotParent();
        // On vérifie que l'utilisateur a les droits de lecture sur le lot concerné
        $this->droitLecture();
        $this->lot = null;

        // Si traitement sur lot
        if($this->traitement->getIdLot())
        {
            $this->lot = $this->traitement->getLot();
            
            $this->commentaireSurEchantillon = null;
            $this->nbCommentaireSurEchantillon = 0;
        }
        // Si traitement sur echantillon
        elseif($this->traitement->getIdEchantillon())
        {
            $this->echantillon = $this->traitement->getEchantillon();
            
            $this->commentaireSurEchantillon = $this->echantillon->getCommentaire();
            $this->nbCommentaireSurEchantillon = $this->commentaireSurEchantillon->count();
        }
        // Si traitement sur mesure
        elseif($this->traitement->getIdMesure())
        {
            $this->mesure = Doctrine_Core::getTable('mesure')->find(array($this->traitement->getIdMesure()));
            
            $this->commentaireSurEchantillon = $this->mesure->getEchantillon()->getCommentaire();
            $this->nbCommentaireSurEchantillon = $this->commentaireSurEchantillon->count();
            $this->commentaireSurMesure = $this->mesure->getCommentaire();
            $this->nbCommentaireSurMesure = $this->commentaireSurMesure->count();
        }
        
        $this->commentaireSurTraitement = $this->traitement->getCommentaire();
        $this->nbCommentaireSurTraitement = $this->commentaireSurTraitement->count();
       
        $this->commentaireSurLot = $this->traitement->getLotParent()->getCommentaire();
        $this->nbCommentaireSurLot = $this->commentaireSurLot->count();
        
    }

//  public function executeNew(sfWebRequest $request)
//  {
//    $this->form = new traitementForm();
//  }
//
//  public function executeCreate(sfWebRequest $request)
//  {
//    $this->forward404Unless($request->isMethod(sfRequest::POST));
//
//    $this->form = new traitementForm();
//
//    $this->processForm($request, $this->form);
//
//    $this->setTemplate('new');
//  }

    public function executeEdit(sfWebRequest $request)
    {
        $this->forward404Unless($this->traitement = Doctrine_Core::getTable('traitement')->find(array($request->getParameter('id_traitement'))), sprintf('Object traitement does not exist (%s).', $request->getParameter('id_traitement')));

        $this->lot = $this->traitement->getLotParent();
        // On vérifie que l'utilisateur a les droits d'écriture sur le lot concerné
        $this->droitEcriture();

        $this->type_traitement = $this->traitement->getTypeTraitement();
        $this->creerFormEdit();

        //On réinitialise la variable lot
        $this->lot = null;
        // Si traitement sur lot
        if($this->traitement->getIdLot() != null)
        {
            $this->lot = $this->traitement->getLot();
            $this->form->traitementLot();
        }
        // Si traitement sur echantillon
        elseif ($this->traitement->getIdEchantillon() != null)
        {
            $this->echantillon = $this->traitement->getEchantillon();
            $this->form->traitementEchantillon();
        }
        // Si traitement sur mesure
        elseif ($this->traitement->getIdMesure() != null)
        {
            $this->mesure = $this->traitement->getMesure();
            $this->form->traitementMesure();
        }
    }

    public function executeUpdate(sfWebRequest $request)
    {
        $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
        $this->forward404Unless($this->traitement = Doctrine_Core::getTable('traitement')->find(array($request->getParameter('id_traitement'))), sprintf('Object traitement does not exist (%s).', $request->getParameter('id_traitement')));

        $this->lot = $this->traitement->getLotParent();
        // On vérifie que l'utilisateur a les droits d'écriture sur le lot concerné
        $this->droitEcriture();

        $this->type_traitement = $this->traitement->getTypeTraitement();
        $this->creerFormEdit();

        $this->params = $request->getParameter($this->form->getName());
        $this->params['updated_at'] = date('Y-m-d H:i:s', time());
        $request->setParameter($this->form->getName(), $this->params);

        // Si traitement sur lot
        if($this->traitement->getIdLot() != null)
        {
            $this->lot = $this->traitement->getLot();
            $this->form->traitementLot();
        }
        // Si traitement sur echantillon
        elseif ($this->traitement->getIdEchantillon() != null)
        {
            $this->echantillon = $this->traitement->getEchantillon();
            $this->form->traitementEchantillon();
        }
        // Si traitement sur mesure
        elseif ($this->traitement->getIdMesure() != null)
        {
            $this->mesure = $this->traitement->getMesure();
            $this->form->traitementMesure();
        }

        $this->params2 = $request->getParameter($this->sousForm->getName());
        $request->setParameter($this->sousForm->getName(), $this->params2);

        $this->processForm($request, $this->form, $this->sousForm);

        $this->setTemplate('edit');
    }

//  public function executeDelete(sfWebRequest $request)
//  {
//    $request->checkCSRFProtection();
//
//    $this->forward404Unless($traitement = Doctrine_Core::getTable('traitement')->find(array($request->getParameter('id_traitement'))), sprintf('Object traitement does not exist (%s).', $request->getParameter('id_traitement')));
//    $traitement->delete();
//
//    $this->redirect('traitement/index');
//  }
  
    protected function creerForm()
    {
        $this->form = new TraitementForm();
        switch ($this->type_traitement->getIdTypeTraitement()) {
            case 1:
                $this->sousForm = new FaisceauForm();
                break;
            case 2:
                $this->sousForm = new PlasmaForm();
                break;
            case 3:
                $this->sousForm = new PolissageDecapageForm();
                break;
            case 4:
                $this->sousForm = new RecuitForm();
                break;
            default:
                $this->sousForm = new AutreForm();
        }
    }
    
    protected function creerFormEdit()
    {
        $this->form = new TraitementForm($this->traitement);
        switch ($this->type_traitement->getIdTypeTraitement()) {
            case 1:
                $this->forward404Unless($this->sousTraitement = Doctrine_Core::getTable('Faisceau')->find(array($this->traitement->getIdTraitement())), sprintf('Object Faisceau does not exist (%s).', $this->traitement->getIdTraitement()));
                $this->sousForm = new FaisceauForm($this->sousTraitement);
                break;
            case 2:
                $this->forward404Unless($this->sousTraitement = Doctrine_Core::getTable('Plasma')->find(array($this->traitement->getIdTraitement())), sprintf('Object Plasma does not exist (%s).', $this->traitement->getIdTraitement()));
                $this->sousForm = new PlasmaForm($this->sousTraitement);
                break;
            case 3:
                $this->forward404Unless($this->sousTraitement = Doctrine_Core::getTable('PolissageDecapage')->find(array($this->traitement->getIdTraitement())), sprintf('Object PolissageDecapage does not exist (%s).', $this->traitement->getIdTraitement()));
                $this->sousForm = new PolissageDecapageForm($this->sousTraitement);
                break;
            case 4:
                $this->forward404Unless($this->sousTraitement = Doctrine_Core::getTable('Recuit')->find(array($this->traitement->getIdTraitement())), sprintf('Object Recuit does not exist (%s).', $this->traitement->getIdTraitement()));
                $this->sousForm = new RecuitForm($this->sousTraitement);
                break;
            default:
                $this->forward404Unless($this->sousTraitement = Doctrine_Core::getTable('Autre')->find(array($this->traitement->getIdTraitement())), sprintf('Object Autre does not exist (%s).', $this->traitement->getIdTraitement()));
                $this->sousForm = new AutreForm($this->sousTraitement);
                break;
        }
    }

  
    public function executeNewTraitementLot(sfWebRequest $request)
    {
        $this->forward404Unless($this->lot = Doctrine_Core::getTable('lot')->find(array($request->getParameter('id_lot'))), sprintf('Object lot does not exist (%s).', $request->getParameter('id_lot')));
        
        // On vérifie que l'utilisateur a les droits d'écriture sur le lot concerné
        $this->droitEcriture();
        
        $this->forward404Unless($this->type_traitement = Doctrine_Core::getTable('TypeTraitement')->find(array($request->getParameter('id_type_traitement'))), sprintf('Object type_traitement does not exist (%s).', $request->getParameter('id_type_traitement')));
        
        $this->creerForm();
        $this->form->traitementLot();
    }
    
    public function executeNewTraitementEchantillon(sfWebRequest $request)
    {
        $this->forward404Unless($this->echantillon = Doctrine_Core::getTable('echantillon')->find(array($request->getParameter('id_echantillon'))), sprintf('Object echantillon does not exist (%s).', $request->getParameter('id_echantillon')));
        
        $this->lot = $this->echantillon->getLot();
        // On vérifie que l'utilisateur a les droits d'écriture sur le lot concerné
        $this->droitEcriture();
        
        $this->forward404Unless($this->type_traitement = Doctrine_Core::getTable('TypeTraitement')->find(array($request->getParameter('id_type_traitement'))), sprintf('Object type_traitement does not exist (%s).', $request->getParameter('id_type_traitement')));
        
        $this->creerForm();
        $this->form->traitementEchantillon();
    }
    
    public function executeNewTraitementMesure(sfWebRequest $request)
    {
        $this->forward404Unless($this->mesure = Doctrine_Core::getTable('mesure')->find(array($request->getParameter('id_mesure'))), sprintf('Object mesure does not exist (%s).', $request->getParameter('id_mesure')));
        
        $this->lot = $this->mesure->getEchantillon()->getLot();
        // On vérifie que l'utilisateur a les droits d'écriture sur le lot concerné
        $this->droitEcriture();
        
        $this->forward404Unless($this->type_traitement = Doctrine_Core::getTable('TypeTraitement')->find(array($request->getParameter('id_type_traitement'))), sprintf('Object type_traitement does not exist (%s).', $request->getParameter('id_type_traitement')));
        $this->echantillon = $this->mesure->getEchantillon();
        
        $this->creerForm();
        $this->form->traitementMesure();
    }
    
    protected function createTraitement()
    {
        $this->params['id_type_traitement'] = $this->type_traitement->getIdTypeTraitement();
        $this->params['id_lot'] = null;
        $this->params['id_echantillon'] = null;
        $this->params['id_mesure'] = null;
        $this->params['ordre'] = null;
        $this->params['created_at'] = date('Y-m-d H:i:s', time());
        $this->params['updated_at'] = date('Y-m-d H:i:s', time());
    }
    
    protected function createSousTraitement(sfWebRequest $request)
    {
        $this->params2 = $request->getParameter($this->sousForm->getName());
        $request->setParameter($this->sousForm->getName(), $this->params2);
    }
    

    public function executeCreateTraitementLot(sfWebRequest $request)
    {
        $this->forward404Unless($request->isMethod(sfRequest::POST));
        $this->forward404Unless($this->lot = Doctrine_Core::getTable('lot')->find(array($request->getParameter('id_lot'))), sprintf('Object lot does not exist (%s).', $request->getParameter('id_lot')));
        
        // On vérifie que l'utilisateur a les droits d'écriture sur le lot concerné
        $this->droitEcriture();
        
        $this->forward404Unless($this->type_traitement = Doctrine_Core::getTable('TypeTraitement')->find(array($request->getParameter('id_type_traitement'))), sprintf('Object type_traitement does not exist (%s).', $request->getParameter('id_type_traitement')));
        
        $this->creerForm();
        $this->form->traitementLot();
        
        
        $this->params = $request->getParameter($this->form->getName());
        $this->createTraitement();
        $this->params['id_lot'] = $this->lot->getIdLot();
        $this->params['date_debut_traitement'] = $this->lot->getDateRecep();
        $this->params['ordre'] = $this->lot->getTraitement()->count()+1;
        $request->setParameter($this->form->getName(), $this->params);
        
        $this->createSousTraitement($request);

        $this->processForm($request, $this->form, $this->sousForm);

        $this->setTemplate('newTraitementLot');
    }
    
    public function executeCreateTraitementEchantillon(sfWebRequest $request)
    {
        $this->forward404Unless($request->isMethod(sfRequest::POST));
        $this->forward404Unless($this->echantillon = Doctrine_Core::getTable('echantillon')->find(array($request->getParameter('id_echantillon'))), sprintf('Object echantillon does not exist (%s).', $request->getParameter('id_echantillon')));
        
        $this->lot = $this->echantillon->getLot();
        // On vérifie que l'utilisateur a les droits d'écriture sur le lot concerné
        $this->droitEcriture();
        
        $this->forward404Unless($this->type_traitement = Doctrine_Core::getTable('TypeTraitement')->find(array($request->getParameter('id_type_traitement'))), sprintf('Object type_traitement does not exist (%s).', $request->getParameter('id_type_traitement')));
        
        $this->creerForm();
        $this->form->traitementEchantillon();
        
        $this->params = $request->getParameter($this->form->getName());
        $this->createTraitement();
        $this->params['id_echantillon'] = $this->echantillon->getIdEchantillon();
        $request->setParameter($this->form->getName(), $this->params);
        
        $this->createSousTraitement($request);

        $this->processForm($request, $this->form, $this->sousForm);

        $this->setTemplate('newTraitementEchantillon');
    }
    
    public function executeCreateTraitementMesure(sfWebRequest $request)
    {
        //$this->forward404Unless($request->isMethod(sfRequest::POST));
        $this->forward404Unless($this->mesure = Doctrine_Core::getTable('mesure')->find(array($request->getParameter('id_mesure'))), sprintf('Object mesure does not exist (%s).', $request->getParameter('id_mesure')));
        
        $this->lot = $this->mesure->getEchantillon()->getLot();
        // On vérifie que l'utilisateur a les droits d'écriture sur le lot concerné
        $this->droitEcriture();
        
        $this->forward404Unless($this->type_traitement = Doctrine_Core::getTable('TypeTraitement')->find(array($request->getParameter('id_type_traitement'))), sprintf('Object type_traitement does not exist (%s).', $request->getParameter('id_type_traitement')));
        $this->echantillon = $this->mesure->getEchantillon();
        
        $this->creerForm();
        $this->form->traitementMesure();
        
        $this->params = $request->getParameter($this->form->getName());
        $this->createTraitement();
        $this->params['id_mesure'] = $this->mesure->getIdMesure();
        $this->params['date_debut_traitement'] = $this->mesure->getDateMesure();
        $request->setParameter($this->form->getName(), $this->params);
        
        $this->createSousTraitement($request);

        $this->processForm($request, $this->form, $this->sousForm);

        $this->setTemplate('newTraitementMesure');
    }
       
  
    protected function processForm(sfWebRequest $request, sfForm $form, sfForm $sousForm)
    {
        if($form->getObject()->isNew()) $new = true; else $new = false;
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid())
        {
            $traitement = $form->save();
            if($traitement->getIdMesure())
            {
                $mesure = Doctrine_Core::getTable('mesure')->find(array($traitement->getIdMesure()));
                $mesure->setIdTraitement($traitement->getIdTraitement());
                $mesure->save();
            }

            $params = $request->getParameter($sousForm->getName());
            $params['id_traitement'] = $traitement->getIdTraitement();
            $request->setParameter($sousForm->getName(), $params);

            $sousForm->bind($request->getParameter($sousForm->getName()), $request->getFiles($sousForm->getName()));

            if($sousForm->isValid())
            {
                $sous = $sousForm->save();
                $this->redirect('traitement/show?id_traitement='.$traitement->getIdTraitement());
            }
            else
            {
                if($new) $traitement->delete();
            }
        }
    }

}
